package com.ulife.song.module.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.form.Group;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.FormGroup;
import com.alibaba.citrus.webx.WebxException;
import com.ulife.common.GroupUtils;
import com.ulife.common.Result;
import com.ulife.common.exception.ManagerException;
import com.ulife.permission.common.TagEnum;
import com.ulife.permission.exceptions.DepartmentErrorException;
import com.ulife.song.dao.dataobject.SongDO;
import com.ulife.song.manager.SongManager;

public class SongAction {

	private static final Logger log = LoggerFactory.getLogger(SongAction.class);
	
	@Autowired
	private SongManager songManager;
	
	/**
	 * 新增歌曲
	 * @param group
	 * @param context
	 * @param nav
	 * @throws WebxException
	 */
	public void doAddSong(@FormGroup("addsong") Group group, Context context, Navigator nav) throws WebxException {
		GroupUtils groupUtil = new GroupUtils(group);
		try {
			SongDO song = new SongDO();
			setSong(song, groupUtil);
			song.setStatus(0);
			Result<SongDO> result = songManager.addSong(song);
			if (result == null || !result.isSuccess() || null == result.getModule()) {
				context.put("error", "系统错误");
				return;
			}
		}
		catch (DepartmentErrorException e) {
			log.error("add song error", e);
		}
		catch (ManagerException e) {
			context.put("error", "系统错误");
			log.error("add song error", e);
		}
		
	}
	
	private void setSong(SongDO song, GroupUtils group) {
		Long songId = group.getLong("songId");
		String songName = group.getString("songName");
		String singer = group.getString("singer");
		String popularity = group.getString("popularity");
		String description = group.getString("description");
		if (StringUtils.isNotBlank(description)) {
			String tag = TagEnum.getTag(description);
			song.setTag(tag);
		}
		String note = group.getString("note");
		song.setSongName(songName);
		song.setDescription(description);
		song.setNote(note);
		song.setSongId(songId);
		song.setPopularity(popularity);
		song.setSinger(singer);
	}

	/**
	 * 删除歌曲
	 * @param group
	 * @param context
	 * @param nav
	 * @throws WebxException
	 */
	public void doDelSong(@FormGroup("delsong") Group group, Context context, Navigator nav) throws WebxException {
		GroupUtils groupUtil = new GroupUtils(group);
		long songId = groupUtil.getLong("id");
		int status = groupUtil.getInt("status");
		try {
			if (status < 0 || status > 1) {
				context.put("error", "系统错误");
				return;
			}
			if (songId >= 0) {
				Result<SongDO> songResult = songManager.getSongById(songId);
				if (songResult != null && songResult.isSuccess() && songResult.getModule() != null) {
					SongDO song = songResult.getModule();
					if (song.getStatus() != status) {
						song.setStatus(status);
						Result<Integer> result = songManager.updateSong(song);
						if (result == null || !result.isSuccess()) {
							context.put("error", "系统错误");
							return;
						}
					}
				}
				else {
					context.put("error", "系统错误");
					return;
				}
			}
		}
		catch (Exception e) {
			context.put("error", "系统错误");
			log.error("del song error, song id = " + songId, e);
		}
	}
	
	/**
	 * 编辑歌曲
	 * @param group
	 * @param context
	 * @param nav
	 * @throws WebxException
	 */
	public void doUpdateSong(@FormGroup("updatesong") Group group, Context context, Navigator nav) throws WebxException {
		GroupUtils groupUtil = new GroupUtils(group);
		long songId = groupUtil.getLong("id");
		try {
			
			if (songId >= 0) {
				Result<SongDO> songResult = songManager.getSongById(songId);
				if (songResult != null && songResult.isSuccess() && songResult.getModule() != null) {
					SongDO song = songResult.getModule();
					setSong(song, groupUtil);
					Result<Integer> updateResult = songManager.updateSong(song);
					if (updateResult == null || !updateResult.isSuccess()) {
						log.error("update song error, song id = " + songId);
						context.put("error", "更新错误");
						return;
					}
				}
			}else {
				context.put("error", "系统错误");
				log.error("update song error, song id = " + songId);
			}
		} 
		catch (DepartmentErrorException e) {
			log.error("update song error, song id = " + songId, e);
		}
		catch (ManagerException e) {
			context.put("error", "系统错误");
			log.error("update song error, song id = " + songId, e);
		}
	}
	
	public void doQuerySong(@FormGroup("querysong") SongDO songDO, Context context, Navigator nav) {
		try {
			List<SongDO> sList = songManager.querySong(songDO);
			context.put("songs", sList);
		} catch (Exception e) {
			log.error("doQuerySupplier error "+songDO.getSinger(),e);
		}
	}
	
}
